<?php

include_once (S_ROOT . './source/function_user.php');
include_once (S_ROOT . './source/api_class.php');

class Shop {
    public $id;
    public $uid;
    public $type;
    public $token;
    public $access;
    public $tpls;
    public $list_name;
    public $apis;
    public $start  = 0;
    public $length = 100;
    
    public function __construct($uid, $type, $id) {
        $this->id    = $id;
        $this->uid   = $uid;
        $this->type  = $type;
        $this->token = $token;
        $this->access= $access;
        $this->tpls       = array('shopzip' => '皮肤包');
        $this->list_name  = array('skin_zips');
        $this->apis       = array(
            'list'      =>  array('getShopzipList'),
            'detail'    =>  array('getShopzipDetail'),
            'add'       =>  array('addShopzip'),
            'edit'      =>  array('editShopzip')
        );
    }
    
    public function execute ($action) {
        
        $do = $this->getDo ($action);
        if ($do) $action = $do;
        
        return $this->$do($action);
    }
    
    // 获取请求方法
    private function getDo ($action) {
        $do = '';
        switch ($action) {
            case in_array($action, $this->apis['list']) :
                $do = 'getShopZipList';
                break;
            case in_array($action, $this->apis['detail']) :
                $do = 'getShopZipInfo';
                break;
            case in_array($action, $this->apis['add']) :
                $do = 'addShopZip';
                break;
            case in_array($action, $this->apis['edit']) :
                $do = 'editShopZipInfo';
                break;
        }
    
        return $do;
    }
    

    /**
     * 获取皮肤包列表
     * @param unknown $action
     * @return string
     */
    public function getShopZipList ($action) {
        // 请求表情列表数据
        $result   = $this->shopListRequest($action);
      
        $list     = $result;

        // 通用变量
        $data = $this->styleVariable($action);
        $data['list'] = $list;

        // 模板渲染到'shop_list.html'页面,用到的变量为render第二个参数中的数据
        return $this->render('shop_zip_list', $data);
    }
    
    /**
     * 获取皮肤包详情
     * @param unknown $action
     * @return string
     */
    public function getShopZipInfo ($action) {
        // 请求表情详情数据
        $result   = $this->shopDetailRequest($action);
        $detail   = $result;
      
        // 通用变量
        $data = $this->styleVariable($action);
        $data['detail'] = $detail;

        // 模板渲染到'shop_detail.html'页面,用到的变量为render第二个参数中的数据
        return $this->render('shop_zip_detail', $data);
    }

    // 编辑皮肤包
    public function editShopZipInfo ($action) {

        // 请求表情详情数据
        $result   = $this->shopEditRequest($action);
        
        if ($result) {
             showmessage('修改成功','admin.php?ac=shopzip&op=getShopzipList');
            exit;
        }

       showmessage('修改失败','admin.php?ac=shopzip&op=getShopzipList');
          exit;
    }
    
    public function addShopZip ($action) {
        $action = 'addShopZip';
        // 请求表情详情数据
        $result   = $this->shopAddRequest($action);
     
        if ($result) {
            showmessage('增加成功','admin.php?ac=shopzip&op=getShopzipList');
            exit;
        }
    
          showmessage('增加失败','admin.php?ac=shopzip&op=getShopzipList');
          exit;
    }
    
    // 请求服务器商店列表数据
    private function shopListRequest ($action) {
        // 组装请求服务器的数据
        $param = array(
            'do'        =>  'shop',
            'format'    =>  'json',
            'action'    =>  $action,
            'uid'       =>  $this->uid,
            'type'      =>  $this->type,
            'id'        =>  $this->id,
            'start'     =>  $this->start,
            'length'    =>  $this->length
        );
        
        return $this->requestApi($action, $param);
    }
    
    // 请求服务器皮肤包详情数据
    private function shopDetailRequest ($action) {
        $param = array(
            'do'        =>  'shop',
            'format'    =>  'json',
            'action'    =>  $action,
            'uid'       =>  $this->uid,
            'id'        =>  $this->id
        );
    
        return $this->requestApi($action, $param);
    }
    
    // 请求服务器编辑皮肤包
    private function shopEditRequest ($action) {
        $param = $this->goodsEditOrAddData($action);

        return $this->requestApi($action, $param);
    }
    
    // 请求服务器添加皮肤包
    private function shopAddRequest ($action) {
        $param = $this->goodsEditOrAddData($action);

        return $this->requestApi($action, $param);
    }
    
    private function goodsEditOrAddData ($action) {
        $info = array(
            'do'        =>  'shop',
            'format'    =>  'json',
            'id'              =>    intval($_GET['id']),
            'pid'             =>    intval($_GET['pid']),
            'status'          =>    intval($_GET['status']),
            'zip_size'        =>    intval($_GET['zip_size']),
            'zip_url'         =>    strval($_GET['zip_url']),
            'platform'        =>    strval($_GET['platform']),
            'min_version'     =>    strval($_GET['min_version']),
            'max_version'     =>    strval($_GET['max_version']),
            'action'          =>    $action
        );
        
        return $info;
    }
    
    // 皮肤包详情数据
    private function goodsDetailData ($result) {
        
    }
    
    // 样式显示相关变量返回
    private function styleVariable ($action) {
        // 通用变量,样式的显示
        $common = $this->apiAndTplName($action);
        
        $result = array(
            'active'        => array($common['goods'] => " class=\"active\""),         // 侧边栏被选择的样式
            'goods'         => ucfirst($common['goods']),   // 商品名
            'goods_name'    => $common['goods_name'],        // 商品名(中文显示)
            'access'        => $this->access
        );
    
        return $result;
    }
    
    // 样式被选定显示
    private function cssShowActive ($attribute) {
        return array($attribute => " class = \" active\"");
    }
    
    // 样式被选定显示
    private function cssShowSelected ($attribute) {
        return array($attribute => " selected");
    }
    
    // 处理返回的数据，返回模板渲染用的数据
    private function resultListData ($result) {
        if (empty($result)) return array();
    
        // 处理返回数据
        foreach ($this->list_name as $tpl) {
            if (!empty($result[$tpl])) {
                return $result[$tpl];
            }
        }
    
        return array();
    }
    
    // 组装返回模板及其他需要的变量
    private function apiAndTplName ($action) {
        if (empty($action)) return array();
    
        // 转小写
        $action = strtolower($action);
        
        // 返回模板名
        foreach ($this->tpls as $good => $name) {
            if (strpos($action, $good)) {
                if ($this->type == 'vip') {
                    $good = 'pro'.$good;
                    return array('goods' => $good, 'goods_name' => $this->tpls [$good]);
                }
                return array('goods' => $good, 'goods_name' => $name);
            }
        }
    
        return array();
    }
    
    // 返回通用字段
    private function assembleReplacedFieldData ($import, $key_arr = array()) {
        if (empty($key_arr)) {
            return $import;
        }
    
        $result = array();
        foreach ($key_arr as $val) {
            if (isset($import[$val])) {
                $result[$val] = strval($import[$val]);
            }
        }
    
        return $result;
    }
    
    // 请求api
    private function requestApi ($action, $param) {
        // 实例化
        $param['action'] = $action;
        
        $result=get_api_content($_SC ['apiurl'], $param);
     
        $result=json_decode($result,TRUE);
        $result=$result['resultData'];
    

   

        return $result;
    }
    
    // 模板渲染
    private function render ($tpl, $data) {
        // 从数组中把变量导入到当前的符号表中
        extract($data, EXTR_SKIP);
    
        // 使用全局变量
        global $_SGLOBAL;
    
        // 开始输出缓冲, 这时PHP停止输出, 在这以后的输出都被转到一个内部的缓冲里.
        ob_start();
    
        try {
            // 加载本地模板
            include template('admin/'.$tpl);
        } catch (Exception $e) {
            // 删除输出缓冲区
            ob_end_clean();
    
            // 抛出异常
            throw $e;
        }
    
        // 结束输出缓冲, 并扔掉缓冲里的内容.
        return ob_get_clean();
    }
}

// 管理员
$id       = 0;
$type     = 'all';
$uid      = $_SESSION['user']['adminUid'];
$action   = 'getSkinzipList';
if ($_GET['id'])   $id     = intval($_GET['id']);
if ($_GET['op'])   $action = strval($_GET['op']);
if ($_GET['type']) $type = strval($_GET['type']);

// 初始化
$data = new Shop($uid, $type,$id);
echo $data->execute($action);
exit;